package com.lzy.websocket.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lzy.websocket.bean.User;
import com.lzy.websocket.bean.Message;
import com.lzy.websocket.service.MessageService;

@Controller
public class MessageController {
	
	@Autowired
	private MessageService messageService;
	
	/**
	 * 获取聊天页面
	 * @param userName
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/chart/{userName}",method = RequestMethod.GET)
	public String getCommunication(@PathVariable("userName") String userName, HttpSession session) {
		User sender = (User) session.getAttribute("user");
		if(sender!=null) {
			List<Message> list = messageService.getMessagesByUserName(sender.getUserName(), userName);
			session.setAttribute("messageList", list);
			session.setAttribute("receiver", userName);
			return "communication";
		}else {
			return "login";
		}
	}
}
